package com.ruoyi.outbound.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.outbound.service.IBmInordergoodslistService;
import com.ruoyi.outbound.service.IBmInorderlistService;
import com.ruoyi.outbound.service.ISysUserService;
import com.ruoyi.system.api.RemoteWarehouseService;
import com.ruoyi.system.api.domain.BmWarehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.outbound.domain.BmOutbound;
import com.ruoyi.outbound.service.IBmOutboundService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 出库管理Controller
 * 
 * @author ruoyi
 * @date 2023-08-02
 */
@RestController
@RequestMapping("/outbound")
public class BmOutboundController extends BaseController
{
    @Autowired
    private IBmOutboundService bmOutboundService;

    @Autowired
    private IBmInorderlistService bmInorderlistService;

    @Autowired
    private RemoteWarehouseService remoteWarehouseService;

    @Autowired
    private IBmInordergoodslistService bmInordergoodslistService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询出库单列表
     */
    @RequiresPermissions("inventory:outbound:list")
    @GetMapping("/list")
    public TableDataInfo list(BmOutbound bmOutbound)
    {
        startPage();
        List<BmOutbound> list = bmOutboundService.selectBmOutboundList(bmOutbound);
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 导出出库单列表
     */
    @RequiresPermissions("inventory:outbound:export")
    @Log(title = "出库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmOutbound bmOutbound)
    {
        List<BmOutbound> list = bmOutboundService.selectBmOutboundList(bmOutbound);
        ExcelUtil<BmOutbound> util = new ExcelUtil<BmOutbound>(BmOutbound.class);
        util.exportExcel(response, list, "出库管理数据");
    }

    /**
     * 获取出库单详细信息
     */
    @RequiresPermissions("inventory:outbound:query")
    @GetMapping(value = "/{outboundCode}")
    public AjaxResult getInfo(@PathVariable("outboundCode") String outboundCode)
    {
        return AjaxResult.success(bmOutboundService.selectBmOutboundByOutboundCode(outboundCode));
    }

    /**
     * 新增出库单
     */
    @RequiresPermissions("inventory:outbound:add")
    @Log(title = "出库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmOutbound bmOutbound, @RequestHeader("Authorization") String authorization)
    {
        String claims = authorization.substring(7);
        String userId = JwtUtils.getUserId(claims);
        bmOutbound.setCreateId(Long.valueOf(userId));
        return toAjax(bmOutboundService.insertBmOutbound(bmOutbound));
    }

    /**
     * 删除出库单
     */
    @RequiresPermissions("inventory:outbound:remove")
    @Log(title = "出库管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{outboundCodes}")
    public AjaxResult remove(@PathVariable String[] outboundCodes)
    {
        return toAjax(bmOutboundService.deleteBmOutboundByOutboundCodes(outboundCodes));
    }
    
    /**
    * @Description: 得到仓库列表
    * @Param:
    * @return:
    * @Author: huang
    * @Date: 2023/8/4
    */
    @GetMapping("/getWarehouseList")
    public AjaxResult getWarehouseList() {
        return AjaxResult.success(remoteWarehouseService.getWarehouseList());
    }

    @Log(title = "出库管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody BmOutbound bmOutbound) {
        return AjaxResult.success(bmOutboundService.updateStatus(bmOutbound));
    }

    @GetMapping("/getOrderList/{warehouseCode}")
    public AjaxResult getOrderList(@PathVariable("warehouseCode") String warehouseCode) {
        return AjaxResult.success(bmInorderlistService.getOrderList(warehouseCode));
    }

    @GetMapping("/getGoodsList/{orderCode}")
    public AjaxResult getGoodsList(@PathVariable("orderCode") String orderCode) {
        return AjaxResult.success(bmInordergoodslistService.selectByPrimaryKey(orderCode));
    }

    @GetMapping("/getUserList")
    public AjaxResult getUserList() {
        return AjaxResult.success(sysUserService.getUserList());
    }
}
